package org.wattdepot.cli.commands;

import java.util.List;
import javax.xml.datatype.XMLGregorianCalendar;
import org.wattdepot.cli.UserCommandInterface;
import org.wattdepot.client.WattDepotClientException;
import org.wattdepot.resource.property.jaxb.Property;
import org.wattdepot.resource.sensordata.jaxb.SensorData;
import org.wattdepot.resource.sensordata.jaxb.SensorDataIndex;
import org.wattdepot.resource.sensordata.jaxb.SensorDataRef;

/**
 * Contains methods that list sensordata for given day.
 * 
 * @author Jarret Mizo, Shaun Ramento
 * 
 */
public class ListSensorDay extends UserCommandInterface implements UserCommands {

  @Override
  public String doCommand(String args) {
    String[] inputString = args.split(" ");
    String output = null;
    try {
      output = listSensorDay(inputString[1], inputString[3]);
    }
    catch (WattDepotClientException e) {
      System.out.println("List summary error.");
    }
    return output;
  }

  /**
   * Prints a table listing all of the sensor data instances for the specified day.
   * 
   * @param sourceName Name of one of the public sources is the system.
   * @param day A date inputed with format YYYY-MM-DD.
   * @throws WattDepotClientException exception that is thrown
   * @return A string with sensordata instances for the specified day.
   */
  public String listSensorDay(String sourceName, String day) throws WattDepotClientException {

    // holds desired source index
    SensorDataIndex sensorDataIndex;
    // holds the list of data
    List<SensorDataRef> sensorDataList;
    // holds the final sensordata String to printout
    StringBuffer sensorData = new StringBuffer(100);
    ValidateDay dayValidation = new ValidateDay();
    // turns day to a XML data type, returns null if cannot change
    XMLGregorianCalendar xmlDate = dayValidation.validateDay(day);
    // split up XML date into date and time
    String[] date;

    // return to command prompt if source doesn't exist
    if (!sourceExists(sourceName)) {
      // return to command prompt
      return "";
    }

    // checks if xmlDate was created if it is null, return to command prompt
    if (xmlDate == null) {
      return "";
    }

    // gets the desired source index
    sensorDataIndex = getClient().getSensorDataIndex(sourceName);
    // holds the list of data
    sensorDataList = sensorDataIndex.getSensorDataRef();
    // splits the XML date up into date and time
    date = xmlDate.toString().split("T");

    for (SensorDataRef data : sensorDataList) {
      String[] sensorDataArray = data.getHref().toString().split("/");
      if (sensorDataArray[sensorDataArray.length - 1].contains(date[0])) {
        XMLGregorianCalendar tempXMLDate =
            dayValidation.validateDay(sensorDataArray[sensorDataArray.length - 1]);
        SensorData tempSensorProperties = getClient().getSensorData(sourceName, tempXMLDate);
        List<Property> propertyList;
        propertyList = tempSensorProperties.getProperties().getProperty();

        sensorData.append(sensorDataArray[sensorDataArray.length - 1] + "  Tool: " + data.getTool()
            + "  Properties: ");

        for (Property property : propertyList) {
          sensorData.append("(" + property.getKey() + " : " + property.getValue() + ")\n");
        }

      }
    }
    // System.out.println(sensorData.toString());
    return sensorData.toString();

  }
}
